package update;

import java.awt.Dimension;
import java.io.File;
import java.net.URL;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class UpdateWindow extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	private static final String THIS_ADD_FILE = Updater.BASE_UPDATE_DIR + Updater.ADD_FILE;
	private static final String THIS_DELETE_FILE = Updater.BASE_UPDATE_DIR + Updater.DELETE_FILE;
	private static final String THIS_DIRECTORIES = Updater.BASE_UPDATE_DIR + Updater.DIRECTORIES;
	
	private static final int WIDTH = 400;
	private static final int FRAME_HEIGHT = 70;
	private static final int BAR_HEIGHT = 50;
	
	private Updater updater;
	private JProgressBar progressBar;
	private int maxSteps, currentStep;
	private List<String> files;
	private String updatedVersion;
	private boolean notLastUpdate;
	
	public UpdateWindow(Updater updater, String version, String latestVersion) {
		super();
		this.updater = updater;
		
		maxSteps = 0;
		files = updater.getList(Updater.getAddedFilesURL(version), THIS_ADD_FILE);
		for(String url : files) {
			try {
				URL u = new URL(Updater.BASE_UPDATE_URL + url);
				int length = u.openConnection().getContentLength() / DownloadProcess.STEP_FACTOR;
				maxSteps += length;
			} catch(Exception e) { e.printStackTrace(); }
		}
		
		List<String> deletedFiles = updater.getList(Updater.getDeletedFilesURL(version), THIS_DELETE_FILE);
		for(String fileName : deletedFiles) {
			File file = new File(fileName);
			file.delete();
		}
		
		List<String> directories = updater.getList(Updater.getDirectoriesURL(version), THIS_DIRECTORIES);
		for(String dirName : directories) {
			File file = new File(dirName);
			file.mkdir();
		}
		
		updatedVersion = updater.getUpdatedVersion(version);
		notLastUpdate = !updatedVersion.equals(latestVersion);
		
		setTitle("Updating Infinite Wars to version " + updatedVersion);
		
		setVisible(true);
		setSize(WIDTH, FRAME_HEIGHT);
		setPreferredSize(new Dimension(WIDTH, FRAME_HEIGHT));
		setLocation(500, 400);
		
		initProgressBar();
		
		pack();
		
		initUpdateProcess();
	}
	
	private void initUpdateProcess() {
		currentStep = 0;
		
		Thread t = new Thread() {
			@Override
			public void run() {
				boolean updaterUpdated = false;
				for(String file : files) {
					if(file.equals(Updater.UPDATER_TEMP_FILE)) updaterUpdated = true;
					
					String url = Updater.BASE_UPDATE_URL + file;
					DownloadProcess process = new DownloadProcess(url, file);
					
					process.addDownloadListener(new DownloadListener() {
						@Override
						public void downloadStatusChanged(DownloadProcess process) {
							currentStep++;
							progressBar.setValue(currentStep);
						}
						
						@Override
						public void downloadStarted(DownloadProcess process) {}
						@Override
						public void downloadFinished(DownloadProcess process) {}
					});
					
					process.startDownload();
				}
				
				updater.saveLatestVersionInfoFile(updatedVersion);
				
				if(!updaterUpdated && notLastUpdate) {
					setVisible(false);
					new Updater(updater.getSystemName());
				} else updater.startGame(updaterUpdated);
			}
		};
		t.start();
	}
	
	private void initProgressBar() {
		progressBar = new JProgressBar(0, maxSteps);
		progressBar.setValue(0);
		progressBar.setSize(WIDTH, BAR_HEIGHT);
		progressBar.setPreferredSize(new Dimension(WIDTH, BAR_HEIGHT));
		progressBar.setStringPainted(true);
		
		getContentPane().add(progressBar);
	}
}